package app.netant.book.mobixs;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import app.netant.NetAntBase;

public class MobixsDetail extends NetAntBase {

	private MobixsEntity entity;
	
	public MobixsDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(MobixsEntity entity){
		this.entity = entity;
	}
	
	public void analyseType(){
		Elements activePart = document.select("body center table[cellspacing='2'] tbody tr td[bgcolor='#F4F4F4'] a");
		if(activePart.size()>1){
			String type = activePart.get(1).text();
			System.out.println("【Type】:"+type);
			this.entity.setNovelType(type);
		}
	}
	
	public void analyseTitle(){
		Elements activePart = document.select("body center table[cellpadding='0'][cellspacing='0'] tbody tr td[valign='top'][align='center'] "
				+ "table[cellpadding='2'][cellspacing='1'][bgcolor='#E0E0E0'] tbody tr[bgcolor='#ffffff'] td[colspan='3'] h1");
		if(activePart.size()>0){
			String title = activePart.get(0).text();
			System.out.println("【Title】:"+title);
			this.entity.setNovelName(title);
			this.entity.setNovelFullName(title);
		}
	}
	
	public void analyseTags(){
		Elements activePart = document.select("body center table[cellpadding='0'][cellspacing='0'] tbody tr td[valign='top'][align='center'] "
				+ "table[cellpadding='2'][cellspacing='1'][bgcolor='#E0E0E0'] tbody tr");
		for(int i=0;i<activePart.size();i++){
			
			Element trs = activePart.get(i);
			Elements tds = trs.select("td");
			
			if(tds.size()>=2){
				
				String tagName = tds.get(0).text().trim();
				String tagValue = tds.get(1).text().trim();
				
				if(!"".equals(tagName)){
					System.out.println("【Tag】:"+tagName+"="+tagValue);
					
					this.entity.setTag(tagName, tagValue);
					
					if("小说介绍".equals(tagName)){				
						String memo = tagValue;
						System.out.println("【Memo】:"+memo);
						this.entity.setNovelMemo(memo);
						
					}
					
					else if("免费下载".equals(tagName)){
						
						String href = tds.get(1).select("a").get(0).attr("href");
						if(href.startsWith("/book")){
							href = "http://www.mobixs.cn" + href;
						}
						
						MobixsDetail detail = new MobixsDetail(href);
						String url = detail.analyseStepHref();
						
						System.out.println("【Href】:"+url);	
						this.entity.setDownPath(url);
						
						//扩展名
						int idx = url.lastIndexOf(".");
						if(idx>0){
							String exp = url.substring(idx);
							String name = this.entity.getNovelName();
							this.entity.setNovelFullName(name+exp);
						}
						
					}
					
					
				}
				
			}
			
			
		}
	}
	
	public String analyseStepHref(){
		
		Elements activePart = document.select("body center table[cellpadding='0'][cellspacing='0'] tbody tr td[valign='top'][align='center'] "
				+ "table[cellpadding='2'][cellspacing='1'][bgcolor='#E0E0E0'] tbody tr td a");
		if(activePart.size()>0){
			String href = activePart.get(0).attr("href");
			return href;
		}
		
		throw new RuntimeException("无法获取下载链接地址!");
		
	}
	
	/**
	 * 信息分析
	 */
	public void analyse(){
		
		this.analyseType();
		this.analyseTitle();
		this.analyseTags();
		
		this.entity.download();
		this.entity.insert();
	}
	
	public static void main(String[] args) {
		
		MobixsDetail detail = new MobixsDetail("http://www.mobixs.cn/book/down-15744.html");
		
		detail.analyseType();
		detail.analyseTitle();
		detail.analyseTags();
		
	}

}
